import { configureStore, combineReducers } from '@reduxjs/toolkit';
import { persistStore, persistReducer } from 'redux-persist';
import storage from 'redux-persist/lib/storage';
import counterSlice from './slice'; // 确保导入正确
import userSlice from './user';


// 2. 使用combineReducers合并reducer
const rootReducer = combineReducers({
  counter: counterSlice,
  user: userSlice
});

// 3. 持久化配置
const persistConfig = {
  key: 'root',
  storage,
  whitelist: ['user'] // 可选
};

// 4. 创建持久化reducer
const persistedReducer = persistReducer(persistConfig, rootReducer);

// 5. 正确配置store
export const store = configureStore({
  reducer: persistedReducer,
  middleware: (getDefaultMiddleware) => 
    getDefaultMiddleware({
      serializableCheck: false // 必须关闭
    })
});

export const persistor = persistStore(store);
export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;